package com.hawthorn.admin.controller.sysmenu;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.service.sysmenu.SysMenuService;
import com.hawthorn.entity.dto.SysUserFavoriteDTO;
import com.hawthorn.entity.dto.SysUserRecentDTO;
import com.hawthorn.platform.context.ContextHolder;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 菜单管理</p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/25 4:19 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Api(tags = {"admin-sysmenu"})
@RestController
@RequestMapping("/sysmenu/")
@Slf4j
public class SysMenuController
{

  @Autowired
  private SysMenuService sysMenuService;

  @ApiOperation(value = "查询->查询最近访问的菜单", notes = "查询最近访问的菜单")
  @ApiImplicitParams({
  })
  @GetMapping(value = "/findUserRecentMenus")

  public List<SysUserRecentDTO> findUserRecentMenus()
  {
    Long userId = ContextHolder.getUserId();
    AssertMyUtil.notNull(userId, BizCode.VARIABLE_NOT_EMPTY, "userid");
    return sysMenuService.findUserRecentMenus(userId);
  }

  @ApiOperation(value = "保存->保存最近访问的菜单", notes = "保存最近访问的菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "userAccount", value = "用户账号", dataTypeClass = String.class), @ApiImplicitParam(name = "userId", value = "用户id", dataTypeClass = Long.class), @ApiImplicitParam(name = "menuId", value = "菜单id", dataTypeClass = Long.class)
  })
  @PutMapping(value = "/saveRecentMenu")
  public boolean saveRecentMenus(@RequestParam(value = "userAccount") String userAccount, @RequestParam(value = "userId") Long userId, @RequestParam(value = "menuId") Long menuId)
  {
    //System.out.println(token);
    boolean del = sysMenuService.delRecentMenu(userId, menuId);
    if (del)
    {
      sysMenuService.saveRecentMenu(userId, userAccount, menuId);
      return true;
    } else
    {
      return false;
    }
  }

  /**
   * <p>todo 查询登录收藏夹的菜单</p>
   * @author: andyten@tom.com
   * @date: 2021/12/10 1:55 PM
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->查询收藏夹的菜单", notes = "查询收藏夹的菜单")
  @ApiImplicitParams({
  })
  @GetMapping(value = "/findUserFavoriteMenus")
  public List<SysUserFavoriteDTO> findUserFavoriteMenus()
  {
    Long userId = ContextHolder.getUserId();
    AssertMyUtil.notNull(userId, BizCode.VARIABLE_NOT_EMPTY, "userid");
    return sysMenuService.findUserFavoriteMenus(userId);
  }

  @ApiOperation(value = "保存->保存收藏夹菜单", notes = "保存收藏夹菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "userAccount", value = "用户账号", dataTypeClass = String.class), @ApiImplicitParam(name = "userId", value = "用户id", dataTypeClass = Long.class), @ApiImplicitParam(name = "menuId", value = "菜单id", dataTypeClass = Long.class)
  })
  @PutMapping(value = "/saveFavoriteMenus")
  public void saveFavoriteMenus(@RequestParam(value = "userAccount") String userAccount, @RequestParam(value = "userId") Long userId, @RequestParam(value = "menuId") Long menuId)
  {
    AssertMyUtil.notNull(userId, BizCode.VARIABLE_NOT_EMPTY, "userid");
    AssertMyUtil.notNull(menuId, BizCode.VARIABLE_NOT_EMPTY, "menuid");
    sysMenuService.saveFavoriteMenu(userId, userAccount, menuId);
  }

  /**
   * <p>todo 移除添加收藏夹</p>
   * @author: andyten@tom.com
   * @date: 2021/12/10 8:51 PM
   * @version: 1.0.1
   */
  @ApiOperation(value = "删除 -> 删除收藏夹菜单", notes = "删除 -> 删除收藏夹菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "userId", value = "用户id", dataTypeClass = Long.class), @ApiImplicitParam(name = "menuId", value = "菜单id", dataTypeClass = Long.class)
  })
  @DeleteMapping(value = "/delFavoriteMenu")
  public void delFavoriteMenu(@RequestParam(value = "userId") Long userId, @RequestParam(value = "menuId") Long menuId)
  {
    AssertMyUtil.notNull(userId, BizCode.VARIABLE_NOT_EMPTY, "userid");
    AssertMyUtil.notNull(menuId, BizCode.VARIABLE_NOT_EMPTY, "menuid");
    sysMenuService.delFavoriteMenu(userId, menuId);
  }

  /**
   * <p>todo 菜单列表查询</p >
   * @author: WCZ
   * @date: 2022/7/7 11:32
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询 -> 菜单列表查询", notes = "查询菜单列表查询")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "查询条件参数", dataTypeClass = String.class)
  })
  @PostMapping(value = "/findMenu")
  public IPage<Map<String, Object>> findMenu(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();
    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysMenuService.findMenu(conditions, page);
  }

  /**
   * <p>todo 新增菜单</p >
   * @author: WCZ
   * @date: 2022/7/7 15:32
   * @version: 1.0.1
   */
  @ApiOperation(value = "新增->新增菜单", notes = "新增菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "保存参数", dataTypeClass = Map.class)
  })
  @PostMapping(value = "/saveMenu")
  public Map<String, Object> saveCompany(@RequestBody Map<String, Object> params)
  {
    return sysMenuService.saveMenu(params);
  }

  /**
   * <p>todo 编辑菜单</p >
   * @author: WCZ
   * @date: 2022/7/8 9:46
   * @version: 1.0.1
   */
  @ApiOperation(value = "编辑->编辑菜单", notes = "编辑菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "更新参数", dataTypeClass = Map.class)
  })
  @PostMapping(value = "/updateMenu/{id}")
  public Map<String, Object> updateMenu(@PathVariable long id, @RequestBody Map<String, Object> params)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");
    return sysMenuService.updateMenu(id, params);
  }

  /**
   * <p>todo 删除菜单</p >
   * @author: WCZ
   * @date: 2022/7/8 9:46
   * @version: 1.0.1
   */
  @ApiOperation(value = "删除->删除菜单", notes = "删除菜单")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class)
  })
  @GetMapping(value = "/removeMenu/{id}")
  public Integer removeMenu(@PathVariable("id") Long id)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");
    return sysMenuService.removeMenu(id);
  }

  /**
   * <p>todo 导出</p >
   * @author: WCZ
   * @date: 2022/7/11 10:20
   * @version: 1.0.1
   */
  @ApiOperation(value = "导出->导出公司信息", notes = "导出公司信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "response", value = "response", dataTypeClass = HttpServletResponse.class),
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/exportMenu")
  public void exportMenu(HttpServletResponse response, @RequestBody Map<String, Object> param) throws IOException
  {
    AssertMyUtil.notNull(param, BizCode.VARIABLE_NOT_EMPTY, "param");
    sysMenuService.exportMenuData(response, "菜单导出", param);

  }

  /**
   * <p>todo 修改状态</p >
   * @author: WCZ
   * @date: 2022/7/11 10:20
   * @version: 1.0.1
   */
  @ApiOperation(value = "修改->状态", notes = "修改状态")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/changeStatus/{id}")
  public Integer changeStatus(@PathVariable long id, @RequestBody String params)
  {
    return sysMenuService.changeStatus(id, params);
  }
}
